//
//  ViewController.m
//  20-NSURLRequestAndResponse
//
//  Created by dllo on 16/8/11.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // NSURL 负责请求地址
    NSURL *baiduURL = [NSURL URLWithString:@"http://www.baidu.com/user/wanghuang/1.html?userName=xxx&pwd=xxx"];
    // 打印URL
    NSLog(@"baiduURL : %@", baiduURL);
    // 打印URL转换成的字符串
    NSLog(@"baiduURL.absoluteString : %@", baiduURL.absoluteString);
    // 打印host(主机地址/域名)
    NSLog(@"baiduURL.host : %@", baiduURL.host);
    // 打印path(主机地址 / 域名 后对应的路径)
    NSLog(@"baiduURL.path : %@", baiduURL.path);
    // query : 打印网址中?后面的东西
    NSLog(@"baiduURL.query : %@", baiduURL.query);
    // scheme : URL所遵循的协议
    NSLog(@"scheme : %@", baiduURL.scheme);
    // resourceSpecifier : 除了协议的部分
    NSLog(@"resourceSpecifier : %@", baiduURL.resourceSpecifier);
    // 不可变请求和可变请求
#pragma mark - 1.NSURLRequest(不可变请求, 什么都不能改)
//    NSURLRequest *baiduRequest = [NSURLRequest requestWithURL:baiduURL];
#pragma mark - 2.NSMutableURLRequest(可变请求)
    // 一般处理网络请求, 都是用可变的请求
    NSMutableURLRequest *baiduMutableRequest = [NSMutableURLRequest requestWithURL:baiduURL];
    // 1>timeoutInterval 超时时间, 一个请求持续多久, 会失败
    baiduMutableRequest.timeoutInterval = 60;
    // 2>HTTPMethod 请求的方式  GET POST PUT DELETE
    baiduMutableRequest.HTTPMethod = @"GET";
    // 3>allHTTPHeaderFields 设置, 网络请求头
    baiduMutableRequest.allHTTPHeaderFields = @{@"User-Agent": @"iOS 10.0 / iPhone8, 1"};
    // 根据字典特性, 相同Key的情况下, 后填入的值, 会覆盖前面的值
    [baiduMutableRequest setValue:@"iOS 10.0 / iPhone8, 1" forHTTPHeaderField:@"User-Agent"];
    // 获取value
    [baiduMutableRequest valueForHTTPHeaderField:@"User-Agent"];
    NSLog(@"%@", baiduMutableRequest.allHTTPHeaderFields);
    // 4>HTTPBody (一般POST请求时, 才会使用)
    // 一般情况下, 都是字典或者字符串转换成的Data
    baiduMutableRequest.HTTPBody = [NSData data];

#pragma mark - NSURLResponse 响应
//    NSURLResponse *response = [[NSURLResponse alloc] init];
    
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:baiduURL statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"data" : [NSData data]}];
    // 1> statusCode 状态码
//    response.statusCode;
    // 2> allHeaderFields 响应头
    // 内容格式 编码
    // Content-Type : application/json; charset:utf-8
    // 时间  日期
    // 缓存的一些内容
    NSLog(@"response.allHeaderFields : %@", response.allHeaderFields);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
